package com.bd.bbd.controller;

import com.alibaba.fastjson.JSON;
import com.bd.bbd.bean.QueryInfo;
import com.bd.bbd.bean.User;
import com.bd.bbd.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class LoginController {
    @Autowired
    UserDao userDao;

    @RequestMapping("/login")
    public String login(@RequestBody User user){
        String flag="error";
        User us=userDao.getUserByMessage(user.getUsername(),user.getPassword());
        System.out.println("user:"+us); //快捷键 sout
        HashMap<String, Object> res = new HashMap<>();
        if(us !=null){
            flag="ok";
        }
        res.put("flag",flag);
        res.put("user",us);
        String res_json = JSON.toJSONString(res);

        return  res_json;
    }

    @RequestMapping("/all")
    public String getalluser(@RequestBody QueryInfo qi){
        String flag="no";
        final int numbers = userDao.getUserCounts("%" + qi.getQuery() + "%");
        int pageStart= (qi.getPageNum()-1)*qi.getPageSize();

        List<User> us=userDao.getAllUser("%" + qi.getQuery() + "%",pageStart,qi.getPageSize());
        System.out.println("user:"+us); //快捷键 sout
        HashMap<String, Object> res = new HashMap<>();
        if(us !=null){
            flag="ok";
        }
        res.put("flag",flag);
        res.put("numbers",numbers);
        res.put("data",us);
        String res_json = JSON.toJSONString(res);

        return  res_json;
    }



}
